package com.itheima.mall.service.smsService;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.pojo.sms.SmsCouponHistory;
import com.itheima.mall.commons.PageBean;
import com.itheima.mall.exception.ConsumerException;
import com.itheima.mall.mapper.smsMapper.SmsCouponHistoryMapper;
import com.itheima.mall.service.smsService.SmsCouponHistoryApi;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class SmsCouponHistoryImpl extends ServiceImpl<SmsCouponHistoryMapper, SmsCouponHistory> implements SmsCouponHistoryApi {
    @Autowired
    private  SmsCouponHistoryMapper smsCouponHistoryMapper;
    //根据优惠卷id,使用状态,订单编号分页获取领取记录
    @Override
    public PageBean<SmsCouponHistory> getList(Integer couponld, String orderSn, Integer pageNum, Integer pageSize, Integer useStatus) {
        IPage<SmsCouponHistory> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<SmsCouponHistory> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(couponld !=null,SmsCouponHistory::getCouponId,couponld);
        queryWrapper.eq(useStatus !=null ,SmsCouponHistory::getUseStatus,useStatus);
        queryWrapper.eq(orderSn != null , SmsCouponHistory::getOrderSn,orderSn);

        smsCouponHistoryMapper .selectPage(page, queryWrapper);
        List<SmsCouponHistory> records = page.getRecords();
        if(ObjectUtil.isEmpty(records)) throw new ConsumerException("暂无数据");
        PageBean<SmsCouponHistory> pageBean = new PageBean(page);
        return pageBean;
    }
}
